package com.timeriver.utils;

import java.util.Random;
import java.util.UUID;

public class UUIDUtil {

    private static final char[] random = {
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
            'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
            'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
            'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
            's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
    };

    /**
     * 获取指定位数的随机码
     * @param length 32位256字节，16位128字节
     * @return
     */
    public static String getSecureRandom(int length) {
        StringBuilder builder = new StringBuilder();
        for (int i=0; i<length ; i++) {
            int bound = new Random().nextInt(random.length);
            builder.append(random[bound]);
        }
        return builder.toString();
    }

    /**
     * 返回随机的UUID值
     * @return
     */
    public static String getRandomUUID() {
        return UUID.randomUUID().toString();
    }

    /**
     * 返回指定前缀的随机UUID
     * @param prefix 前缀
     * @return
     */
    public static String getRandomUUID(String prefix, String separator) {
        StringBuffer buffer = new StringBuffer();
        buffer.append(prefix);
        buffer.append(separator);
        buffer.append(getRandomUUID());

        return buffer.toString();
    }

    /**
     * 返回时间戳
     * @param prefix
     * @param separator
     * @return
     */
    public static String getTimestampUUID(String prefix, String separator) {
        StringBuffer buffer = new StringBuffer();
        buffer.append(prefix);
        buffer.append(separator);
        buffer.append(System.currentTimeMillis());

        return buffer.toString();
    }
}
